#include "uart_process.h"
#include "uart_send.h"
#include "user_cfg.h"


static uint8_t uarty_buf[7];
static uart_cfg_t uarty_cfg = {
    uarty_buf,
    sizeof(uarty_buf)
};

//串口初始化
void Send_Init(void)
{
	Uarty_Init();
    Uart_Send_Init(UARTY, &uarty_cfg);
}

/**
 * @description: 串口数据打包，间隔50ms轮询发送给主控
 * @param {uint16_t} key_value 触摸按键值
 */
void Uart_Data_Pack(uint16_t key_value)
{
    uint8_t buf[7];
    buf[0] = 0x55;
    buf[1] = 0x00;
    buf[2] = 0x00;
    buf[3] = (uint8_t)(key_value>>8);
    buf[4] = (uint8_t)(key_value);
    buf[5] = (uint8_t)(buf[0] + buf[1] + buf[2] + buf[3] + buf[4]);
    buf[6] = 0xFB;
    Uart_Send(UARTY, buf, sizeof(buf));
}

void Uarty_Init(void)
{
    NVIC_InitType NVIC_InitStructure;
    GPIO_InitType GPIO_InitStructure;
	UART_InitType UART_InitStructure;

	/* Enable GPIO clock */
	RCC_APB_Peripheral_Clock_Enable(UARTy_GPIO_CLK);
    /* Enable UARTy and UARTz Clock */
	UARTy_APBxClkCmd(UARTy_CLK);
    /* Initialize GPIO_InitStructure */

    /* Enable the UARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel         = UARTy_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = NVIC_PRIORITY_1;
    NVIC_InitStructure.NVIC_IRQChannelCmd      = ENABLE;
    NVIC_Initializes(&NVIC_InitStructure);

    /* Configure UARTy Tx as alternate function push-pull */
    GPIO_Structure_Initialize(&GPIO_InitStructure);
    GPIO_InitStructure.Pin            = UARTy_TxPin;
    GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Alternate = UARTy_Tx_GPIO_AF;
    GPIO_Peripheral_Initialize(UARTy_GPIO, &GPIO_InitStructure);
    // /* Configure UARTx Rx as alternate function push-pull */
    GPIO_InitStructure.Pin            = UARTy_RxPin;
    GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_INPUT;
    GPIO_InitStructure.GPIO_Alternate = UARTy_Rx_GPIO_AF;
    GPIO_Peripheral_Initialize(UARTy_GPIO, &GPIO_InitStructure);

    /* UARTy and UARTz configuration */
    UART_InitStructure.BaudRate   = 9600;
    UART_InitStructure.WordLength = UART_WL_8B;
    UART_InitStructure.StopBits   = UART_STPB_1;
    UART_InitStructure.Parity     = UART_PE_NO;
    UART_InitStructure.Mode       = UART_MODE_TX;
	UART_Initializes(UARTy, &UART_InitStructure);
	UART_Interrput_Enable(UARTy, UART_INT_RXDNE);
    UART_Interrput_Enable(UARTy, UART_INT_TXDE);
	UART_Enable(UARTy);
}



